#!/usr/bin/python
# author Yu
# 2023年06月20日
import socket


class Server:
    def __init__(self, ip, port):
        self.addr = (ip, port)  # 服务器地址
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #
        self.server_socket.bind(self.addr)  # 绑定服务器地址
        self.server_socket.listen(10)  # 监听客户端连接

    def connect(self):
        client_socket=None#初始值为None
        while True:
            try:
                self.server_socket.setblocking(False)  # 设置非阻塞
                client_socket, client_addr = self.server_socket.accept()  # 接收客户端连接
            except Exception as e:#如果没有客户端连接，就会报异常
                if client_socket:#如果有客户端连接
                    client_socket.setblocking(False)#设置非阻塞
                    try:
                        data_bytes = client_socket.recv(64)#接收客户端数据
                        if data_bytes:#如果有数据
                            print(data_bytes.decode('utf-8'))#打印数据
                        else:
                            print('连接已经断开')
                            return
                    except Exception as e:
                        pass

server=Server('',9999)
server.connect()